package com.heihei.imgpreview;

import com.heihei.combine.util.DirFlagUtil;
import com.heihei.config.ConfigProperties;
import com.heihei.constant.MessageConstant;
import com.heihei.imgpreview.dto.ImgInfoDTO;
import com.heihei.imgpreview.dto.LineDTO;
import com.heihei.util.FileUtilF;
import com.heihei.util.StrUtilF;
import org.apache.commons.collections4.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *  读取所有图片，制作预览图（限制每张预览图）
 *
 * @version 1.0
 * @since JDK1.8
 * @author duanhengfei
 * @date 2021年02月15日 12:09:44
 */
public class ImgDividePreviewStart {
    private static Integer IMG_INDEX;

    public static void main(String[] args) throws IOException {
        start();
    }

    /**
     * 方法描述: 开始执行
     *
     * @param
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年02月15日 12:11:44
     */
    public static void start() throws IOException {
        IMG_INDEX = 1;
        System.out.println("请输入源目录：");
        File validSrcDir = FileUtilF.getValidSrcDir();
        Integer flag = DirFlagUtil.getFlag();
        if(flag == 1) {
            handleDir(validSrcDir, ConfigProperties.ACCROSS_LINE_EVERY_IMG, ConfigProperties.VERTICAL_LINE_EVERY_IMG);
        } else if(flag == 2) {
            File[] childrenList = validSrcDir.listFiles();
            boolean contentFlag = false;
            for (File child : childrenList) {
                if(child.isDirectory()) {
                    contentFlag = true;
                    handleDir(child, ConfigProperties.ACCROSS_LINE_EVERY_IMG, ConfigProperties.VERTICAL_LINE_EVERY_IMG);
                }
            }
            if(!contentFlag) {
                System.out.println("该文件夹下没有目录");
            }
        } else {
            System.out.println(MessageConstant.PARAM_ILLEGAL);
        }
    }

    /**
     * 方法描述: 处理文件夹
     *
     * @param parentDir
     * @param accrossLineEveryImg
     * @param verticalLineEveryImg
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年02月15日 15:04:23
     */
    private static void handleDir(File parentDir, Integer accrossLineEveryImg, Integer verticalLineEveryImg) throws IOException {
        List<File> imgFileList = FileUtilF.readAllImgFile(parentDir);
        List<ImgInfoDTO> imgInfoDTOList = GeneratePreviewImg.addImg(imgFileList, 0);
        List<LineDTO> lineDTOList = GeneratePreviewImg.getResultLineDto(imgInfoDTOList);
        //预览图parentDir
        File previewPDir = getPreviewPDir(parentDir);
        //存储横向current图片
        List<LineDTO> accrossCurrLineList = new ArrayList<>();
        //存储纵向current图片
        List<LineDTO> verticalCurrLineList = new ArrayList<>();
        //存储横纵current图片
        List<LineDTO> accVerCurrLineList = new ArrayList<>();
        for (LineDTO lineDTO : lineDTOList) {
            int flag = judgeAccrossOrVertical(lineDTO);
            if(flag == 1) {
                accrossCurrLineList.add(lineDTO);
            } else if(flag == 2) {
                verticalCurrLineList.add(lineDTO);
            } else if(flag == 3) {
                accVerCurrLineList.add(lineDTO);
            }
        }
        //写图片
        writeImg(accrossCurrLineList, verticalCurrLineList, accVerCurrLineList, previewPDir, accrossLineEveryImg, verticalLineEveryImg);
        System.out.println(MessageConstant.EVERYTHING_IS_OK);
    }

    private static void writeImg(List<LineDTO> accrossCurrLineList, List<LineDTO> verticalCurrLineList, List<LineDTO> accVerCurrLineList, File previewPDir, Integer accrossLineEveryImg, Integer verticalLineEveryImg) throws IOException {
        if(CollectionUtils.isNotEmpty(accrossCurrLineList) && CollectionUtils.isNotEmpty(verticalCurrLineList)) {
            int indexFlag = 1;
            while(accrossCurrLineList.size() >= accrossLineEveryImg && verticalCurrLineList.size() >= verticalLineEveryImg) {
                if(indexFlag == 1) {
                    writeSeveralLineImg(accrossCurrLineList, accrossLineEveryImg, previewPDir);
                    indexFlag = 2;
                } else {
                    writeSeveralLineImg(verticalCurrLineList, verticalLineEveryImg, previewPDir);
                    indexFlag = 1;
                }
            }
        }
        while(accrossCurrLineList.size() >= accrossLineEveryImg){
            writeSeveralLineImg(accrossCurrLineList, accrossLineEveryImg, previewPDir);
        }
        while(verticalCurrLineList.size() >= verticalLineEveryImg) {
            writeSeveralLineImg(verticalCurrLineList, verticalLineEveryImg, previewPDir);
        }
        //剩下的合并到一起吧
        List<LineDTO> last = new ArrayList<>();
        last.addAll(accrossCurrLineList);
        last.addAll(verticalCurrLineList);
        last.addAll(accVerCurrLineList);
        GeneratePreviewImg.writeImg(last, new File(previewPDir, StrUtilF.getNumberString(IMG_INDEX++, 4) + MessageConstant.IMG_STYLE));

        //        GeneratePreviewImg.writeImg(lineDTOList, );
    }

    public static void writeSeveralLineImg(List<LineDTO> imgLineList, Integer maxLineCount, File previewPDir) throws IOException {
        List<LineDTO> subList = imgLineList.subList(imgLineList.size() - maxLineCount, imgLineList.size());
        GeneratePreviewImg.writeImg(subList, new File(previewPDir, StrUtilF.getNumberString(IMG_INDEX++, 4) + MessageConstant.IMG_STYLE));
        for (int i = 0; i < maxLineCount; i++) {
            imgLineList.remove(imgLineList.size() - 1);
        }
    }

    /**
     * 方法描述: 获取previewDir
     *
     * @param dir
     * @Return {@link File}
     * @throws
     * @author duanhengfei
     * @date 2021年02月15日 13:35:13
     */
    private static File getPreviewPDir(File dir) {
        File result = new File(dir.getParent(), dir.getName() + "_preview");
        int index = 1;
        while(result.exists()){
            result = new File(dir.getParent(), dir.getName() + "_preview" + StrUtilF.getNumberString(index++, 4));
        }
        result.mkdirs();
        return result;
    }

    /**
     * 方法描述: 该行是横或者纵
     *
     * @param lineDTO
     * @Return {@link int} 1 全横， 2 全纵， 3 横纵相间， 0 无图片
     * @throws
     * @author duanhengfei
     * @date 2021年02月15日 13:15:48
     */
    private static int judgeAccrossOrVertical(LineDTO lineDTO) {
        List<ImgInfoDTO> imgInfoList = lineDTO.getImgInfoList();
        boolean verticalFlag = false;
        boolean accrossFlag = false;
        for (ImgInfoDTO img : imgInfoList) {
            if(img.getAccrossOrVerticalFlag() == 1) {
                accrossFlag = true;
            } else if(img.getAccrossOrVerticalFlag() == 2) {
                verticalFlag = true;
            }
            if(verticalFlag && accrossFlag) {
                break;
            }
        }
        if(!verticalFlag && accrossFlag) {
            return 1;
        } else if(verticalFlag && !accrossFlag) {
            return 2;
        } else if(verticalFlag && accrossFlag) {
            return 3;
        } else {
            return 0;
        }
    }
}